<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div>
        <!-- 搜索 -->
        <label class="el-form-item-label">进出人员</label>
        <el-input  clearable
          placeholder="名称"
          style="width: 185px"
          class="filter-item" v-model="query.userName" />
        <label class="el-form-item-label" >实验室</label>
        <el-select
          v-model="query.labId"
          clearable
          style="width: 185px;"
          placeholder="实验室"
          class="filter-item"
        >
          <el-option
            v-for="item in labs"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
        <label class="el-form-item-label">类型</label>
        <el-select
          v-model="query.type"
          clearable
          style="width: 185px;"
          placeholder="类型"
          class="filter-item"
        >
        <el-option label="进入" :value="1" />
        <el-option label="出去" :value="2" />
        </el-select>
        <rrOperation :crud="crud" />
          <!-- <el-button
            class="filter-item"
            size="mini"
            type="success"
            icon="el-icon-search"
            @click="onSearch"
          >搜索</el-button>
          <el-button
            class="filter-item"
            size="mini"
            type="warning"
            icon="el-icon-refresh-left"
            @click="onReset"
          >重置</el-button> -->
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog
        :close-on-click-modal="false"
        :before-close="crud.cancelCU"
        :visible.sync="crud.status.cu > 0"
        :title="crud.status.title"
        width="500px"
      >
        <el-form
          ref="form"
          :model="form"
          :rules="rules"
          size="small"
          label-width="80px"
        >
          <el-form-item label="进出人员" prop="userName">
            <el-input v-model="form.userName" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="实验室" prop="labId">
          <el-select
            v-model="form.labId"
            style="width: 370px;"
            clearable
            filterable
            :remote-method="getLabs"
            :loading="searchLoading"
            placeholder="请选择实验室"
          >
            <el-option
              v-for="item in labs"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
            </el-select>
          </el-form-item>
          <el-form-item label="类型" prop="type">
            <el-select
              v-model="form.type"
              style="width: 370px;"
              placeholder="请选择类型"
            >
              <el-option label="进入" :value="1" />
              <el-option label="出去" :value="2" />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button
            :loading="crud.status.cu === 2"
            type="primary"
            @click="crud.submitCU"
            >确认</el-button
          >
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table
        ref="table"
        :data="crud.data"
        size="small"
        style="width: 100%"
        v-loading="crud.loading"
      >
        <el-table-column prop="userName" label="进出人员"  align="center"/>
        <el-table-column prop="labName" label="实验室"  align="center"/>
        <el-table-column prop="type" label="类型" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.type == 1">进入</span>
            <span v-else-if="scope.row.type == 2">出去</span>
            <span v-else>未知</span>
          </template>
        </el-table-column>
        <el-table-column
          v-if="
            checkPer([
              'admin',
              'labSwipeCardRecord:edit',
              'labSwipeCardRecord:del',
            ])
          "
          label="操作"
          width="160px"
          align="center"
        >
          <template slot-scope="scope">
            <div>
              <el-button
                size="mini"
                icon="el-icon-view"
                type="info"
                @click="handleLook(scope.row)"
              />
              <!-- <udOperation :data="scope.row" :permission="permission" /> -->
            </div>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <el-pagination
        :page-size.sync="page.size"
        :total="total"
        :current-page.sync="page.page"
        style="margin-top: 8px;"
        layout="total, prev, pager, next, sizes"
        @current-change="pageChangeHandler"
      />
    </div>
    <el-dialog
      :close-on-click-modal="false"
      :visible.sync="view"
      title="记录详情"
      width="700px"
    >
      <div style="display: flex">
        <el-form ref="form" :model="form" size="small" label-width="80px">
          <el-form-item prop="userName" label="人员信息">
            <el-input v-model="dialogInfo.name" style="width: 370px" disabled/>
          </el-form-item>
          <el-form-item prop="labName" label="实验室">
            <el-input v-model="dialogInfo.labName" style="width: 370px" disabled/>
          </el-form-item>
          <el-form-item prop="createdAt" label="时间">
            <el-input v-model="dialogInfo.time" style="width: 370px" disabled/>
          </el-form-item>
          <!-- <el-form-item prop="updatedAt" label="离开时间">
            <el-input v-model="form.updatedAt" style="width: 370px" disabled/>
          </el-form-item> -->
        </el-form>
        <div style="float: right; margin-left: 50px">
          <el-avatar title="图片" :size="150" shape="square" fit="fill" :src="dialogInfo.url"/>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="text" @click="view = false">取消</el-button>
        <el-button type="primary" @click="view = false">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import crudLabSwipeCardRecord, { searchId,getOpendoorrecordListAPI} from "@/api/labSwipeCardRecord";
import CRUD, { presenter, header, form, crud } from "@crud/crud";
import rrOperation from "@crud/RR.operation";
import crudOperation from "@crud/CRUD.operation";
import udOperation from "@crud/UD.operationDelete";
import pagination from "@crud/Pagination";
import { mapState } from "vuex";
import { get, findAdmin } from "@/api/lab";
import { allNamelist } from "@/api/user";
import moment from "moment";

const defaultForm = {
  id: null,
  adminName: null,
  type: null,
  picture: null,
  labId: null,
  createdAt: null,
  updatedAt: null,
  isDeleted: null,
  userName:null,
};
export default {
  name: "LabSwipeCardRecord",
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: "实验室刷卡记录",
      url: "api/opendoorrecord",
      // url: "api/labSwipeCardRecord",
      idField: "id",
      sort: "id,desc",
      crudMethod: { ...crudLabSwipeCardRecord },
      optShow: {
        add: true,
        edit: false,
        del: false,
        download: false,
        reset: true,
      },
    });
  },
  data() {
    return {
      permission: {
        add: ["admin", "labSwipeCardRecord:add"],
        edit: ["admin", "labSwipeCardRecord:edit"],
        del: ["admin", "labSwipeCardRecord:del"],
      },
      rules: {
        userName: [{ required: true, message: "人员信息不能为空", trigger: "blur" }],
        createdAt: [{ required: true, message: "进入时间不能为空", trigger: "blur" }],
        updatedAt: [{ required: true, message: "离开时间不能为空", trigger: "blur" }],
        type: [{ required: true, message: "类型不能为空", trigger: "change" }],
        labId: [{ required: true, message: "实验室不能为空", trigger: "blur" }],
      },
      queryTypeOptions: [
        { key: "userName", display_name: "进出人员" },
        { key: "type", display_name: "类型" },
        { key: "labId", display_name: "实验室" },
      ],
      labs: [],
      admins: [],
      view: false,
      baseUrl: process.env.VUE_APP_BASE_API,
      picSrc:'',
      nameList:[],
      searchLoading: false,
      searchName:null,
      page:{
        size:10,
        page:0
      },
      total:0,
      tableList: [],
      loading: false,
      dialogInfo:{
        name:'',
        time:'',
        url:'',
        labName:''
      },
      labId: null,
      type: null
    };
  },
  computed: {
    ...mapState("hjuser", ["systemSearchInfoId"]),
  },
  created() {
    this.getLabs();
    this.getAdmins();
    this.getName();
    this.getOpendoorrecordList()
  },
  methods: {
    getName(){
      allNamelist().then((response) => {
          this.nameList = response.content;
          console.log(this.nameList);
        })
        .catch((_) => {
          // console.log(response);
        });
    },
    getLabs() {
      this.searchLoading = true
      get()
        .then((response) => {
          this.searchLoading = false
          this.labs = response;
        })
        .catch((_) => {
          this.searchLoading = false
          console.log(response);
        });
    },
    getAdmins() {
      findAdmin()
        .then((response) => {
          this.admins = response;
        })
        .catch((_) => {
          console.log(response);
        });
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true;
    },
    // async [CRUD.HOOK.afterRefresh]() {
      // if (this.systemSearchInfoId) {
      //   try {
      //     const res = await searchId(this.systemSearchInfoId);
      //     this.crud.data = res.content;
      //   } finally {
      //     this.$store.commit("hjuser/updatedsystemSearchInfoId", "");
      //   }
      // }
      // return true;
    // },
   handleLook(row) {
      this.view = true;
      this.dialogInfo.name = row.userName;
      this.dialogInfo.labName = row.labName;
      this.dialogInfo.time = moment(row.eventTime).format('YYYY-MM-DD hh:mm:ss');
      this.dialogInfo.url = row.picUri;
      // this.picSrc = this.baseUrl + row.picture.substring(4);
    },
    async getOpendoorrecordList() {
      this.loading = true;
      try {
        const { content,totalElements} = await getOpendoorrecordListAPI({...this.page,userName:this.searchName,labId:this.labId,type:this.type})
      this.tableList = content
      this.total = totalElements
      } catch (error) {}
      this.loading = false
    },
    onSearch(){
       this.page.page =1
       this.getOpendoorrecordList()
    },
    onReset(){
      this.page.page =1
      this.searchName =null
      this.type = null
      this.labId = null
      this.getOpendoorrecordList()
    },
    pageChangeHandler(page){
      this.page.page =page
      this.getOpendoorrecordList()
    }
  },
};
</script>

<style scoped>
</style>
